﻿using MediatR;
using S.Parturition.API.Application.Command;
using S.Parturition.Domain;
using S.Parturition.Domain.Model;

namespace S.Parturition.API.Application.CommandHandlers
{
    public class UpdateLambStateAllHandler : IRequestHandler<UpdateLambStateAllCommand, bool>
    {

        public readonly MyDbContext dbContext;

        public UpdateLambStateAllHandler(MyDbContext dbContext)
        {
            this.dbContext = dbContext;
        }

        public async Task<bool> Handle(UpdateLambStateAllCommand request, CancellationToken cancellationToken)
        {
            List<LambRegistrationModel> lamblist = dbContext.LambRegistrationModel.Where(x => request.EarTag.Contains(x.EarTag)).ToList();

            if (lamblist.Any())
            {
                foreach (var lamb in lamblist)
                {
                    lamb.SheepStatus = lamb.SheepStatus == true ? false : true;
                }
                dbContext.UpdateRange(lamblist);
                return await dbContext.SaveChangesAsync() > 0;
            }

            return false;
        }
    }
}
